<?php
namespace app\modelOperate;

use think\facade\Log;

/**
 * ClassName:模型操作基类
 * @auditor LeiJunXiang
 * @time 2024/09/26
 */
class ModelOpeBase extends \think\Model
{
    //自动设置时间戳字段
    protected $autoWriteTimestamp = true;
    //主键
//    protected $pk = 'objKey';
    // 设置当前模型对应的完整数据表名称
    protected $table = '';
    //对象key
    protected $objKey;




    public function __construct()
    {
        parent::__construct();
        $this->table = basename(str_replace('\\', '/', get_class($this)));
        $this->table = str_replace(['Ope'], '', $this->table);
        $this->table = $this->camelToSnake($this->table);
    }
    /**
     * 将驼峰命名法转换为下划线命名法
     * @param string $input
     * @return string
     */
    protected function camelToSnake($input)
    {
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
        $ret = $matches[0];
        foreach ($ret as &$match) {
            $match = $match === strtoupper($match) ? strtolower($match) : lcfirst($match);
        }
        return implode('_', $ret);
    }

    /**
     * 获取db对象
     * @return void
     */
    protected function getDb(){
        $db = \think\facade\Db::table($this->table);
        return $db;
    }

    /**
     * 保存数据
     * @param $data
     * @return void
     */
    public function saveData($data)
    {
        $db = $this->getDb();
        $flag = $db->insert($data);
        return $flag;
    }


    /**
     * @return mixed
     */
    public function getObjKey()
    {
        return $this->objKey;
    }

    /**
     * @param mixed $objKey
     */
    public function setObjKey($objKey): void
    {
        $this->objKey = $objKey;
    }



}
